package com.sltx.service.impl;

import com.sltx.config.TencentSmsScript;
import com.sltx.dao.aaa.AuditMapper;
import com.sltx.service.AuditService;
import com.sltx.util.PageData;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AuditServiceImpl implements AuditService {

    @Value("${spring.datasource.aaa.jdbc-url}")
    private String url;
    @Value("${spring.datasource.aaa.driver-class-name}")
    private String dd;
    @Value("${spring.datasource.aaa.username}")
    private String username;
    @Value("${spring.datasource.aaa.password}")
    private String password;

    Connection conn = null;
    Statement stmt = null;

    public AuditServiceImpl() {
    }

    @Autowired
    private AuditMapper auditMapper;

    @Override
    public Map<String, Object> getUploadedALL(PageData pd) {
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());
        Integer startPage = ((pageSize * (pageNo - 1)));
        pd.put("startPage", startPage.toString());

        Map<String, Object> result = new HashMap<>();

        result.put("count", auditMapper.getNum());
        result.put("list", auditMapper.getUploadedALL(pd));
        return result;
    }

    @Override
    public List<Map<String, Object>> Download(PageData pd) {
        Map<String, Object> map = auditMapper.getConnum(pd);
        List<Map<String, Object>> list = auditMapper.taskEnt(pd);
        List<Map<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("taskNum", map.get("task_num"));
            map1.put("entname", list.get(i).get("entname"));
            map1.put("uniscid", list.get(i).get("uniscid"));
            map1.put("connum", map.get("connum"));
            map1.put("userName", map.get("user_name"));
            result.add(map1);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> DownloadQueryc(PageData pd) {
        return auditMapper.getQueryc(pd);
    }

    @Autowired
    private TencentSmsScript tencentSmsScript;


    @Override
    public Object insertExcel1(List<Map<String, Object>> list1, PageData pd) {
        try {
            Class.forName(dd);
            conn = DriverManager.getConnection(url, username, password);
            // 关闭自动执行
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            for (int i = 0; i < list1.size(); i++) {
                String sql = "insert into f_gqct_result(task_num,entname,uniscid,query_c,industryco_d,esdate,user_name) values ('" +
                        list1.get(i).get("任务编号") + "','" +
                        list1.get(i).get("企业名称") + "','" +
                        list1.get(i).get("统一社会信用代码") + "','" +
                        list1.get(i).get("关联单位(分析对象)") + "','" +
                        list1.get(i).get("行业大类") + "','" +
                        list1.get(i).get("企业工商注册时间") + "','" +
                        list1.get(i).get("用户名") + "');";
                System.out.println(sql);
                stmt.addBatch(sql);
                if (i % 100 == 0 && i != 0) {
                    stmt.executeBatch();
                    conn.commit();
                    stmt.clearBatch();
                }
            }
            stmt.executeBatch();
            conn.commit();
            stmt.clearBatch();
            close();
            return "上传成功";
        } catch (Exception e) {
            return e.toString();
        }
    }

    @Override
    public Object insertExcel2(List<Map<String, Object>> list2, PageData pd) {
        try {
            Class.forName(dd);
            conn = DriverManager.getConnection(url, username, password);
            conn.setAutoCommit(false);
            stmt = conn.createStatement();
            Map<String, Object> map = auditMapper.getUserName(pd);
            for (int i = 0; i < list2.size(); i++) {
                String sql = "insert into f_grct_gql(task_num,entname,uniscid,query_c,ent_onp,zb_onp,ent_twp,zb_twp,ent_thp,zb_thp,ent_fop,zb_fop,ent_fip,zb_fip,ent_sip,zb_sip,rl_num) values ('" +
                        list2.get(i).get("任务编号") + "','" +
                        list2.get(i).get("企业名称") + "','" +
                        list2.get(i).get("统一社会信用代码") + "','" +
                        list2.get(i).get("关联单位(分析对象)") + "','" +
                        list2.get(i).get("一级母公司") + "','" +
                        list2.get(i).get("一级母公司占比") + "','" +
                        list2.get(i).get("二级母公司") + "','" +
                        list2.get(i).get("二级母公司占比") + "','" +
                        list2.get(i).get("三级母公司") + "','" +
                        list2.get(i).get("三级母公司占比") + "','" +
                        list2.get(i).get("四级母公司") + "','" +
                        list2.get(i).get("四级母公司占比") + "','" +
                        list2.get(i).get("五级母公司") + "','" +
                        list2.get(i).get("五级母公司占比") + "','" +
                        list2.get(i).get("六级母公司") + "','" +
                        list2.get(i).get("六级母公司占比") + "','" +
                        list2.get(i).get("关联单位所处级数") + "');";
                System.out.println(sql);
                stmt.addBatch(sql);
                if (i % 100 == 0 && i != 0) {
                    stmt.executeBatch();
                    conn.commit();
                    stmt.clearBatch();
                }
            }
            stmt.executeBatch();
            conn.commit();
            stmt.clearBatch();
            close();
            String phone = (String) map.get("user_tel");
            if (!StringUtils.isEmpty(phone)) {
                String[] phoneSplit = phone.split(",");
                if (!phoneSplit[0].isEmpty()) {
                    tencentSmsScript.send(phoneSplit[0], map.get("task_name").toString());
                    }
                if (!phoneSplit[1].isEmpty()) {
                    tencentSmsScript.send(phoneSplit[1], map.get("task_name").toString());
                }
                if (!phoneSplit[2].isEmpty()) {
                    tencentSmsScript.send(phoneSplit[2], map.get("task_name").toString());
                }

            }
            int num = auditMapper.getEntNew(pd);
            Date date = new Date();
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            pd.put("finishDate", dateFormat1.format(date));
            pd.put("num", num);
            auditMapper.updateStatus(pd);
            return "上传成功";
        } catch (Exception e) {
            auditMapper.deledeTaskResult(pd);
            return e.toString();
        }
    }

    @Override
    public void upDateTime(PageData pd) {
        auditMapper.upDateTime(pd);
    }



    public void close() {
        try {
            this.conn.close();
            this.stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    //嵌套接口
    public Map<String, Object> getUploadedALL2(PageData pd) {
        int pageSize = Integer.parseInt(pd.get("pageSize").toString());
        int pageNo = Integer.parseInt(pd.get("pageNo").toString());
        Integer startPage = pageSize * (pageNo - 1);
        pd.put("startPage", startPage.toString());
        Map<String, Object> result = new HashMap();
        result.put("count", this.auditMapper.getNum2());
        result.put("list", this.auditMapper.getUploadedALL2(pd));
        return result;
    }

    public List<Map<String, Object>> Download2(PageData pd) {
        Map<String, Object> map = this.auditMapper.getConnum2(pd);
        List<Map<String, Object>> list = this.auditMapper.taskEnt2(pd);
        List<Map<String, Object>> result = new ArrayList();

        for(int i = 0; i < list.size(); ++i) {
            Map<String, Object> map1 = new HashMap();
            map1.put("taskNum", map.get("task_num"));
            map1.put("entname", ((Map)list.get(i)).get("entname"));
            map1.put("uniscid", ((Map)list.get(i)).get("uniscid"));
            map1.put("connum", map.get("connum"));
            map1.put("userName", map.get("user_name"));
            result.add(map1);
        }

        return result;
    }

    public List<Map<String, Object>> DownloadQueryc2(PageData pd) {
        return this.auditMapper.getQueryc2(pd);
    }


    @SneakyThrows
    public Object insertExcel12(List<Map<String, Object>> list1, PageData pd) {
        try {

            Class.forName(this.dd);
            this.conn = DriverManager.getConnection(this.url, this.username, this.password);
            this.conn.setAutoCommit(false);
            this.stmt = this.conn.createStatement();
            this.auditMapper.deledeTaskResult2(pd);

            for(int i = 0; i < list1.size(); ++i) {
                String sql = "insert into f_gqct_result(task_num,entname,uniscid,query_c,industryco_d,esdate,user_name) values ('" + ((Map)list1.get(i)).get("任务编号") + "','" + ((Map)list1.get(i)).get("企业名称") + "','" + ((Map)list1.get(i)).get("统一社会信用代码") + "','" + ((Map)list1.get(i)).get("关联单位(分析对象)") + "','" + ((Map)list1.get(i)).get("行业大类") + "','" + ((Map)list1.get(i)).get("企业工商注册时间") + "','" + ((Map)list1.get(i)).get("用户名") + "');";
                System.out.println(sql);
                this.stmt.addBatch(sql);
                if (i % 100 == 0 && i != 0) {
                    this.stmt.executeBatch();
                    this.conn.commit();
                    this.stmt.clearBatch();
                }
            }

            this.stmt.executeBatch();
            this.conn.commit();
            this.stmt.clearBatch();
            this.close();
            return "上传成功";
        } catch (Exception var5) {
            this.conn.rollback();
            return var5.toString();
        }

    }

    @SneakyThrows
    public Object insertExcel22(List<Map<String, Object>> list2, PageData pd) {
        try {
            try {
                Class.forName(this.dd);
                this.conn = DriverManager.getConnection(this.url, this.username, this.password);
                this.conn.setAutoCommit(false);
                this.stmt = this.conn.createStatement();
                Map<String, Object> map = this.auditMapper.getUserName2(pd);

                int num;
                for(num = 0; num < list2.size(); ++num) {
                    String sql = "insert into f_grct_gql(task_num,entname,uniscid,query_c,ent_onp,zb_onp,ent_twp,zb_twp,ent_thp,zb_thp,ent_fop,zb_fop,ent_fip,zb_fip,ent_sip,zb_sip,rl_num)" + " values ('" + ((Map)list2.get(num)).get("任务编号") + "','" + ((Map)list2.get(num)).get("企业名称") + "','" + ((Map)list2.get(num)).get("统一社会信用代码") + "','" + ((Map)list2.get(num)).get("关联单位(分析对象)") + "','" + ((Map)list2.get(num)).get("一级母公司") + "','" + ((Map)list2.get(num)).get("一级母公司占比") + "','" + ((Map)list2.get(num)).get("二级母公司") + "','" + ((Map)list2.get(num)).get("二级母公司占比") + "','" + ((Map)list2.get(num)).get("三级母公司") + "','" + ((Map)list2.get(num)).get("三级母公司占比") + "','" + ((Map)list2.get(num)).get("四级母公司") + "','" + ((Map)list2.get(num)).get("四级母公司占比") + "','" + ((Map)list2.get(num)).get("五级母公司") + "','" + ((Map)list2.get(num)).get("五级母公司占比") + "','" + ((Map)list2.get(num)).get("六级母公司") + "','" + ((Map)list2.get(num)).get("六级母公司占比") + "','" + ((Map)list2.get(num)).get("关联单位所处级数") + "');";
                    System.out.println(sql);
                    this.stmt.addBatch(sql);
                    if (num % 100 == 0 && num != 0) {
                        this.stmt.executeBatch();
                        this.conn.commit();
                        this.stmt.clearBatch();
                    }
                }
                this.stmt.executeBatch();
                this.conn.commit();
                this.stmt.clearBatch();
                this.close();
                if (!StringUtils.isEmpty(map.get("user_tel"))) {
                    this.tencentSmsScript.send(map.get("user_tel").toString(), map.get("task_name").toString());
                }
                num = this.auditMapper.getEntNew2(pd);
                Date date = new Date();
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
                pd.put("finishDate", dateFormat1.format(date));
                pd.put("num", num);
                this.auditMapper.updateStatus2(pd);
                return "上传成功";
            } catch (Exception var7) {
                this.auditMapper.deledeTaskResult2(pd);
                this.conn.rollback();
                return var7.toString();
            }
        } catch (Throwable var8) {
            throw var8;
        }
    }

    public void upDateTime2(PageData pd) {
        this.auditMapper.upDateTime2(pd);
    }

}
